﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MultiUploadHandler.aspx.cs"
    Inherits="CARMS.MultiUploadHandler" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<style>

.UploaderItem
{
    width:300px;
    }
</style>
    <script type="text/javascript">
        var uploadBoxes = [];
        var uploadForm = null;

        function initDocument() {  
            uploadForm = document.getElementById("uploadForm");

            addUploadBox();

            resizeContainer();
        }

        function UploadBox_Changed(e) {
            if (this.value.length == 0 && uploadBoxes.length > 0) {
                if (uploadBoxes[uploadBoxes.length - 1] != this) {
                    for (var i = 0; i < uploadBoxes.length; i++) {
                        if (uploadBoxes[i] == this) {
                            uploadBoxes.splice(i, 1);

                            break;
                        }
                    }

                    uploadForm.removeChild(this);

                    resizeContainer();
                }
            }
            else {
                if (uploadBoxes[uploadBoxes.length - 1].value.length > 0) {
                    addUploadBox();

                    resizeContainer();
               }
            }
        }

        function addUploadBox() {
            var newBox = document.createElement("input");

            newBox.type = "file";
            newBox.className = "uploadBox";
            newBox.name = "uploadBox" + uploadBoxes.length;
            newBox.className = "UploadInput";

            newBox.className = "UploaderItem";
            newBox.ChangedHandler = UploadBox_Changed;

            AddHandler(newBox, "propertychange", "ChangedHandler");
            AddHandler(newBox, "keyup", "ChangedHandler");
            AddHandler(newBox, "click", "ChangedHandler");
            AddHandler(newBox, "change", "ChangedHandler");

            uploadForm.appendChild(newBox);
            uploadBoxes.push(newBox);
        }

        function resizeContainer() {
//            var iframes = window.parent.document.getElementsByTagName("iframe")
//            var iframe;

//            for (var i = 0; i < iframes.length; i++) {
//                if (iframes[i].contentWindow.location == window.location) {
//                    iframes[i].style.height = document.body.offsetHeight + "px";
//                    break;
//                }
//            }
 
            var iframes = window.parent.document.getElementsByTagName("iframe")
            var iframe;
            var sumHeight;
            sumHeight=0;
            
                for (var i = 0; i < iframes.length; i++)
                 {
                    if (iframes[i].contentWindow.location == window.location) 
                    { 
                         for(var j=0;j<uploadBoxes.length;j++)
                          {
                            sumHeight=sumHeight+26;
                          }
                    
                        iframes[i].style.height = sumHeight + "px";
                        break;
                    }
                } 
          }

        function AddHandler(eventSource, eventName, handlerName, eventTarget) {
            if (eventTarget == null)
                eventTarget = eventSource;

            if (eventSource.addEventListener) {
                eventSource.addEventListener(eventName, function(e) { eventTarget[handlerName](e); }, false);
            }
            else if (eventSource.attachEvent) {
                eventSource.attachEvent("on" + eventName, function(e) { eventTarget[handlerName](e); });
            }
            else {
                var originalHandler = eventSource["on" + eventName];

                if (originalHandler) {
                    eventSource["on" + eventName] = function(e) { originalHandler(e); eventTarget[handlerName](e); };
                }
                else {
                    eventSource["on" + eventName] = eventTarget[handlerName];
                }
            }
        }		
    </script>

</head>
<body onload="initDocument()">
    <form id="uploadForm" name="uploadForm" runat="server" enctype="multipart/form-data">
    </form>
</body>
</html>
